﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Linq;
using System.Web.UI.HtmlControls;

namespace park.Equipment
{
    public partial class ParkingSpaces : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            {
                //InitTree();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string str_json = GetParkingSpacesInfo();
            InitTree(str_json);
        }

        private string GetParkingSpacesInfo()
        {
            #region TEST ...
    //        string str_json = ""
    //+ "{"
    //+ "    DefaultParkingSpaces: ["
    //+ "{"
    //+ "    Collector: \"收集器01\","
    //+ "    Repeaters: ["
    //+ "        { Name: \"中继器01\", ParkingSpaces: [{ PS: \"车位1\", Enable: true }, { PS: \"车位2\", Enable: true}] },"
    //+ "        { Name: \"中继器02\", ParkingSpaces: [{ PS: \"车位1\", Enable: true }, { PS: \"车位2\", Enable: true}] },"
    //+ "        { Name: \"中继器03\", ParkingSpaces: [{ PS: \"车位1\", Enable: true }, { PS: \"车位2\", Enable: true}] }"
    //+ "        ]"
    //+ "},"
    //+ "    {"
    //+ "        Collector: \"收集器02\","
    //+ "        Repeaters: ["
    //+ "        { Name: \"中继器04\", ParkingSpaces: [{ PS: \"车位1\", Enable: true }, { PS: \"车位2\", Enable: true}] },"
    //+ "        { Name: \"中继器05\", ParkingSpaces: [{ PS: \"车位1\", Enable: true }, { PS: \"车位2\", Enable: true}] },"
    //+ "        { Name: \"中继器06\", ParkingSpaces: [{ PS: \"车位1\", Enable: true }, { PS: \"车位2\", Enable: true}] }"
    //+ "        ]"
    //+ "    }]"
    //+ "}";
            #endregion

            string str_json = string.Empty;
            string city_id = Request.Params["ddl_City"];
            string area_id = Request.Params["ddl_Area"];
            string street_id = Request.Params["ddl_Street"];

            cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();
            str_json = sl.GetParkingSpacesEnable(city_id,area_id,street_id);
            // 当页面回发以后，重新绑定并选择城区、片区信息  cityId , areaId, streetId
           // Page.ClientScript.RegisterStartupScript(GetType(), "initDropDownList", "<script> cityId = '" + city_id + "' ; areaId = '" + area_id + "'; streetId = '" + street_id + "';\r\nBindCity(cityId, areaId, streetId);</script>");

            return str_json;
        }

        private void InitTree(string str_json)
        {             
            JObject json = JObject.Parse(str_json);

            Table treeTable = new Table();
            treeTable.ID = "treeTable";

            for (int i = 0; i < json["DefaultParkingSpaces"].Count(); i++)
            {
                TableRow tr1 = new TableRow();

                tr1.ID = "node-" + (i + 1).ToString();

                TableCell tdText1 = new TableCell();
                TableCell tdChecked1 = new TableCell();

                tdText1.Text = json["DefaultParkingSpaces"][i]["Collector"].ToString();
                //CheckBox cbx1 = new CheckBox();
                //cbx1.ID = "cbxCollector_" + i.ToString();
                //tdChecked1.Controls.Add(cbx1);

                tr1.Cells.Add(tdText1);
                tr1.Cells.Add(tdChecked1);
                treeTable.Rows.Add(tr1);

                for (int j = 0; j < json["DefaultParkingSpaces"][i]["Repeaters"].Count(); j++)
                {
                    TableRow tr2 = new TableRow();

                    tr2.ID = "node-" + (i + 1).ToString() + "-" + (j + 1).ToString();
                    //tr2.Attributes.Add("class", "child-of-node-" + (j + 1).ToString() + "  initialized parent expanded");
                    tr2.Attributes.Add("class", "child-of-node-" + (i + 1).ToString());

                    TableCell tdText2 = new TableCell();
                    TableCell tdChecked2 = new TableCell();

                    tdText2.Text = json["DefaultParkingSpaces"][i]["Repeaters"][j]["Name"].ToString();
                    //CheckBox cbx2 = new CheckBox();
                    //cbx2.ID = "cbxRepeaters_" + i.ToString();
                    //tdChecked2.Controls.Add(cbx2);

                    tr2.Cells.Add(tdText2);
                    tr2.Cells.Add(tdChecked2);
                    treeTable.Rows.Add(tr2);
                    for (int k = 0; k < json["DefaultParkingSpaces"][i]["Repeaters"][j]["ParkingSpaces"].Count(); k++)
                    {
                        TableRow tr3 = new TableRow();

                        tr3.ID = "node-" + (i + 1).ToString() + "-" + (j + 1).ToString() + "-" + (k + 1).ToString();
                        tr3.Attributes.Add("class", "child-of-node-" + (i + 1).ToString()+"-"+(j+1).ToString());

                        TableCell tdText3 = new TableCell();
                        TableCell tdChecked3 = new TableCell();

                        tdText3.Text = json["DefaultParkingSpaces"][i]["Repeaters"][j]["ParkingSpaces"][k]["PS"].ToString();
                        CheckBox cbx3 = new CheckBox();
                        cbx3.ID = string.Format("cbxPS_{0}_{1}_{2}",i,j,k);
                        cbx3.Checked = bool.Parse(json["DefaultParkingSpaces"][i]["Repeaters"][j]["ParkingSpaces"][k]["Enable"].ToString());
                        cbx3.Attributes.Add("onclick", "CheckBox_Click()");
                        
                 
                        tdChecked3.Controls.Add(cbx3);

                        tr3.Cells.Add(tdText3);
                        tr3.Cells.Add(tdChecked3);
                        treeTable.Rows.Add(tr3);
                    }
                }
            }

            this.PanelTree.Controls.Add(treeTable);

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string str_json = GetParkingSpacesInfo();
            JObject json = JObject.Parse(str_json);

            // 0_0_0:false,0_0_1:false,0_1_0:false,0_1_1:false,0_2_0:true,0_2_1:true,1_0_0:true,1_0_1:true,1_1_0:true,1_1_1:true,1_2_0:true,1_2_1:true

            string value = this.HiddenField1.Value;
            if (!string.IsNullOrEmpty(value))
            {
                string[] cbxs_Info = value.Split(',');
                foreach (string cbx in cbxs_Info)
                {
                    string[] cbx_key_value = cbx.Split(':');

                    string[] index = cbx_key_value[0].Split('_');
                    if (index.Length == 3) // length = 3 说明是车位级别
                    {
                        int i = int.Parse(index[0]);
                        int j = int.Parse(index[1]);
                        int k = int.Parse(index[2]);

                        json["DefaultParkingSpaces"][i]["Repeaters"][j]["ParkingSpaces"][k]["Enable"] = cbx_key_value[1];
                    }               
                }
            }

            cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();
            bool flag = sl.SaveParkingSpacesEnable(json.ToString());

            if (flag)
            {
                Page.ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "<script>alert('数据保存成功')</script>");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "<script>alert('数据保存失败，请重试 ...')</script>");
            }

            InitTree(json.ToString()); // 重新绑定树
        }

    }
}